package com.example.javapracticaltraining.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.javapracticaltraining.entity.R;
import com.example.javapracticaltraining.entity.pojo.Elderly;
import com.example.javapracticaltraining.entity.vo.request.ElderlyRequestVO;
import com.example.javapracticaltraining.entity.vo.response.ElderlyResponseVO;
import com.example.javapracticaltraining.service.ElderlyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 老人管理控制器
 */
@Tag(name = "老人管理", description = "提供老人信息的增删改查功能，支持老人入住和退住管理")
@RestController
@RequestMapping("/elderly")
public class ElderlyController {

    @Autowired
    private ElderlyService elderlyService;

    /**
     * 分页查询老人信息
     * @param page 页码
     * @param pageSize 每页大小
     * @param name 姓名（可选，用于筛选）
     * @param status 状态（可选，用于筛选）
     * @param roomNumber 房间号（可选，用于筛选）
     * @return 分页后的老人信息列表
     */
    @Operation(summary = "分页查询老人信息", description = "根据条件分页查询老人信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功获取老人信息分页列表")
    })
    @GetMapping("/page")
    public R<IPage<ElderlyResponseVO>> getElderlyByPage(
            @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页记录数", example = "10") @RequestParam(defaultValue = "10") int pageSize,
            @Parameter(description = "姓名，支持模糊查询") @RequestParam(required = false) String name,
            @Parameter(description = "状态（在住、退住）") @RequestParam(required = false) String status,
            @Parameter(description = "房间号") @RequestParam(required = false) String roomNumber) {

        Page<Elderly> pageInfo = new Page<>(page, pageSize);
        IPage<ElderlyResponseVO> result = elderlyService.pageElderly(pageInfo, name, status, roomNumber);
        return R.success(result);
    }

    /**
     * 根据ID获取老人信息
     * @param elderlyId 老人ID
     * @return 老人信息
     */
    @Operation(summary = "获取老人详情", description = "根据ID获取老人详细信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "成功获取老人信息"),
        @ApiResponse(responseCode = "404", description = "未找到该老人")
    })
    @GetMapping("/{elderlyId}")
    public R<ElderlyResponseVO> getElderly(
            @Parameter(description = "老人ID", required = true) @PathVariable Integer elderlyId) {
        ElderlyResponseVO elderly = elderlyService.getElderlyById(elderlyId);
        if (elderly != null) {
            return R.success(elderly);
        }
        return R.failure(404, "未找到该老人");
    }

    /**
     * 添加新的老人信息（入住）
     * @param requestVO 老人信息请求
     * @return 操作结果
     */
    @Operation(summary = "老人入住", description = "创建新的老人信息，完成入住登记")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "老人入住登记成功"),
        @ApiResponse(responseCode = "400", description = "请求参数有误")
    })
    @PostMapping
    public R<ElderlyResponseVO> addElderly(@RequestBody ElderlyRequestVO requestVO) {
        try {
            ElderlyResponseVO responseVO = elderlyService.createElderly(requestVO);
            return R.success(responseVO);
        } catch (IllegalArgumentException e) {
            return R.failure(400, e.getMessage());
        }
    }

    /**
     * 更新老人信息
     * @param requestVO 老人信息请求
     * @return 操作结果
     */
    @Operation(summary = "更新老人信息", description = "根据ID更新老人信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "老人信息更新成功"),
        @ApiResponse(responseCode = "400", description = "请求参数有误"),
        @ApiResponse(responseCode = "404", description = "未找到该老人")
    })
    @PutMapping
    public R<ElderlyResponseVO> updateElderly(@RequestBody ElderlyRequestVO requestVO) {
        try {
            ElderlyResponseVO responseVO = elderlyService.updateElderly(requestVO);
            return R.success(responseVO);
        } catch (IllegalArgumentException e) {
            return R.failure(400, e.getMessage());
        }
    }

    /**
     * 老人退住
     * @param elderlyId 老人ID
     * @param dischargeDate 退住日期
     * @return 操作结果
     */
    @Operation(summary = "老人退住", description = "处理老人退住，更新状态和退住日期")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "老人退住处理成功"),
        @ApiResponse(responseCode = "400", description = "请求参数有误"),
        @ApiResponse(responseCode = "404", description = "未找到该老人")
    })
    @PostMapping("/{elderlyId}/discharge")
    public R<String> dischargeElderly(
            @Parameter(description = "老人ID", required = true) @PathVariable Integer elderlyId,
            @Parameter(description = "退住日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date dischargeDate) {
        try {
            boolean result = elderlyService.dischargeElderly(elderlyId, dischargeDate);
            if (result) {
                return R.success("老人退住处理成功");
            } else {
                return R.failure(500, "老人退住处理失败");
            }
        } catch (IllegalArgumentException e) {
            return R.failure(400, e.getMessage());
        }
    }
}